package com.mo.xi.ge.info.help;

import android.util.Base64;
import java.io.UnsupportedEncodingException;
import kotlin.text.Charsets;

/**
 * date:2024/1/30
 **/
public class Base64Util {


    public static String toBase64(byte[] value) {
        if (value == null) return "";
        try {
            return new String(Base64.encode(value, 0, value.length, Base64.NO_WRAP), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }

    public static String toBase64(String value) {
        if (value == null) return "";
        try {
            byte[] bytes = value.getBytes(Charsets.UTF_8);
            return new String(Base64.encode(bytes, 0, bytes.length, Base64.NO_WRAP), "UTF-8");
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }

    public static byte[] decode(String value) {
        if (value == null) return null;
        try {
            return Base64.decode(value.getBytes(Charsets.UTF_8), 0, value.length(), Base64.NO_WRAP);
        } catch (Exception e) {
            return null;
        }
    }

    public static byte[] decode(byte[] value) {
        if (value == null) return null;
        try {
            return Base64.decode(value, 0, value.length, Base64.NO_WRAP);
        } catch (Exception e) {
            return null;
        }
    }
}
